package com.houtai.service.user;

import com.houtai.base.ResultVo;
import com.houtai.bean.User;
import com.houtai.bean.dto.user.*;
import com.houtai.bean.vo.user.UserLoginVo;
import com.houtai.common.util.CommonUtil;
import com.houtai.common.util.SendMessageUtil;
import com.houtai.dao.UserDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class UserService {
    private static Logger logger = Logger.getLogger(UserService.class);

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * @Purpose:    修改头像
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo updateHeadImg(String head_img, String token) {
        String logInfo =  ":updateHeadImg:";
        logger.info("======" + logInfo + "begin======");
        ResultVo vo = new ResultVo();
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            user.setHead_img(head_img);
            userDao.updateUserHead(user);
            vo.setMark("0");
            vo.setTip("成功");

        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    发送短信
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo sendMessage(String phone_no) {
        String logInfo =  ":sendMessage:";
        logger.info("======" + logInfo + "begin======");
        ResultVo vo = new ResultVo();
        try {

            String code = CommonUtil.randomString(4);
            logger.info("验证码：" + code);
            if (SendMessageUtil.setRegisterSms(phone_no, code)) {
                redisTemplate.opsForValue().set(phone_no, code, 5, TimeUnit.MINUTES);
                vo.setMark("0");
                vo.setTip("成功");
            } else {
                vo.setMark("1");
                vo.setTip("短信发送失败");
            }


        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    验证是否注册
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo isRegister(String phone_no) {
        String logInfo =  ":isRegister:";
        logger.info("======" + logInfo + "begin======");
        ResultVo vo = new ResultVo();
        try {
            User user = userDao.findUserByPhoneNo(phone_no);
            if (user != null) {
                vo.setMark("0");
                vo.setTip("已注册");
            } else {
                vo.setMark("1");
                vo.setTip("未注册");
            }


        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    注册
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo register(UserReisgerDto dto) {
        String logInfo =  ":register:";
        logger.info("======" + logInfo + "begin======");
        ResultVo vo = new ResultVo();
        try {
            User user = userDao.findUserByPhoneNo(dto.getPhone_no());
            if (user != null) {
                vo.setMark("1");
                vo.setTip("已注册");
            } else {
                if (redisTemplate.opsForValue().get(dto.getPhone_no()) != null) {
                    String code = (String) redisTemplate.opsForValue().get(dto.getPhone_no());
                    if (code.equals(dto.getCode())) {
                        User register = new User();
                        register.setNick_name(dto.getPhone_no().substring(0, 3) + "****" + dto.getPhone_no().substring(dto.getPhone_no().length() - 4, dto.getPhone_no().length()));
                        register.setPhone_no(dto.getPhone_no());
                        register.setUser_type(dto.getUser_type());
                        register.setUser_pass(dto.getUser_pass());
                        register.setRegister_year(CommonUtil.getYear());
                        register.setRegister_month(CommonUtil.getMonth());
                        register.setRegister_day(CommonUtil.getDay());
                        register.setPhone_no(dto.getPhone_no());
                        register.setHead_img("https://mydataapp.oss-cn-hangzhou.aliyuncs.com/image/school/20200226012843474.png");
                        userDao.insertUser(register);
                        vo.setMark("0");
                        vo.setTip("注册成功");
                    } else {
                        vo.setMark("1");
                        vo.setTip("验证码不正确");
                    }
                } else {
                    vo.setMark("1");
                    vo.setTip("验证码已失效");
                }
            }
        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    手机登陆
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized UserLoginVo phone_login(UserPhoneLoginDto dto) {
        String logInfo =  ":phone_login:";
        logger.info("======" + logInfo + "begin======");
        UserLoginVo vo = new UserLoginVo();
        try {
            User user = userDao.findUserByPhoneNo(dto.getPhone_no());
            if (user != null) {
                if (redisTemplate.opsForValue().get(dto.getPhone_no()) != null) {
                    String code = (String) redisTemplate.opsForValue().get(dto.getPhone_no());
                    if (code.equals(dto.getCode())) {
                        String token = CommonUtil.getToken();
                        redisTemplate.opsForValue().set(token, user, 1, TimeUnit.DAYS);
                        vo.setMark("0");
                        vo.setTip("成功");
                        vo.setToken(token);
                        vo.setHead_img(user.getHead_img());
                        vo.setNick_name(user.getNick_name());
                        vo.setIs_auth(user.getIs_auth());
                        vo.setUser_type(user.getUser_type());
                    } else {
                        vo.setMark("1");
                        vo.setTip("验证码不正确");
                    }
                } else {
                    vo.setMark("1");
                    vo.setTip("验证码已失效");
                }
            } else {
                vo.setMark("1");
                vo.setTip("手机号码未注册");
            }
        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    密码登陆
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized UserLoginVo pass_login(UserPassLoginDto dto) {
        String logInfo =  ":phone_login:";
        logger.info("======" + logInfo + "begin======");
        UserLoginVo vo = new UserLoginVo();
        try {
            User user = userDao.findUserByPhoneNo(dto.getPhone_no());
            if (user != null) {
                if (user.getUser_pass().equals(dto.getUser_pass())) {
                    String token = CommonUtil.getToken();
                    redisTemplate.opsForValue().set(token, user, 1, TimeUnit.DAYS);
                    vo.setMark("0");
                    vo.setTip("成功");
                    vo.setToken(token);
                    vo.setHead_img(user.getHead_img());
                    vo.setNick_name(user.getNick_name());
                    vo.setIs_auth(user.getIs_auth());
                    vo.setUser_type(user.getUser_type());
                } else {
                    vo.setMark("1");
                    vo.setTip("密码不正确");
                }
            } else {
                vo.setMark("1");
                vo.setTip("手机号码未注册");
            }
        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    忘记密码
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo forget_pass(UserForgetPassDto dto) {
        String logInfo =  ":forget_pass:";
        logger.info("======" + logInfo + "begin======");
        ResultVo vo = new ResultVo();
        try {
            User user = userDao.findUserByPhoneNo(dto.getPhone_no());
            if (user != null) {
                if (redisTemplate.opsForValue().get(dto.getPhone_no()) != null) {
                    String code = (String) redisTemplate.opsForValue().get(dto.getPhone_no());
                    if (code.equals(dto.getCode())) {
                        user.setUser_pass(dto.getUser_pass());
                        userDao.updateUserPass(user);
                        vo.setMark("0");
                        vo.setTip("成功");
                    } else {
                        vo.setMark("1");
                        vo.setTip("验证码不正确");
                    }
                } else {
                    vo.setMark("1");
                    vo.setTip("验证码已失效");
                }
            } else {
                vo.setMark("1");
                vo.setTip("手机号码未注册");
            }
        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    修改密码
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo update_pass(UserUpdatePassDto dto, String token) {
        String logInfo =  ":update_pass:";
        logger.info("======" + logInfo + "begin======");
        ResultVo vo = new ResultVo();
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            if (user.getUser_pass().equals(dto.getOld_user_pass())) {
                user.setUser_pass(dto.getUser_pass());
                userDao.updateUserPass(user);
                vo.setMark("0");
                vo.setTip("成功");

            } else {
                vo.setMark("1");
                vo.setTip("密码不正确");
            }


        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
}
